﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace hse_2013_e_booking
{
    public class Hotel
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int stars;

        public int Stars
        {
            get { return stars; }
            set { stars = value; }
        }
        private string room;

        public string Room
        {
            get { return room; }
            set { room = value; }
        }
        private double price;

        public double Price
        {
            get { return price; }
            set { price = value; }
        }
        public Hotel(string name, int stars, string room, double price)
        {
            this.name = name;
            this.stars = stars;
            this.room = room;
            this.price = price;
        }


        public static void Вывод(List<Hotel> a, DataGridView d)
        {
            d.Rows.Clear();
            for (int i = 0; i < a.Count; i++)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(d);
                row.Cells[0].Value = a[i].Name;
                row.Cells[1].Value = a[i].Stars;
                row.Cells[2].Value = a[i].Room;
                row.Cells[3].Value = a[i].Price;
                d.Rows.Add(row);
            }
        }
        public static void Вывод(List<Hotel> a, DataGridView d,int str1)
        {
            d.Rows.Clear();
            for (int i = 0; i < a.Count; i++)
            {if (str1==a[i].Stars)
                {
                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(d);
                row.Cells[0].Value = a[i].Name;
                row.Cells[1].Value = a[i].Stars;
                row.Cells[2].Value = a[i].Room;
                row.Cells[3].Value = a[i].Price;
                d.Rows.Add(row);
                }
            }
        }
                public static void Вывод(List<Hotel> a, DataGridView d,string str1,string str2,string str3)
        {
            d.Rows.Clear();
            for (int i = 0; i < a.Count; i++)
            {

                bool sov = true;
                if ((str3 == a[i].Name) && (str3 != "")) sov = false;
                if (str1 != "") { if ((int.Parse(str1) == a[i].Stars)) sov = false; };
                if ((str2 == a[i].Room)) sov = false;
                 
                if ((sov))
                 {

                DataGridViewRow row = new DataGridViewRow();
                row.CreateCells(d);
                   
                    row.Cells[0].Value = a[i].Name;

                    row.Cells[1].Value = a[i].Stars;

                    row.Cells[2].Value = a[i].Room;
                    row.Cells[3].Value = a[i].Price;

               
                    d.Rows.Add(row);
            }
                
            }
        }
        
        public static List<Hotel> spisok = new List<Hotel>();
        public static void load(StreamReader sr)
        {
            Hotel hot;
            string[] A = new string[4]; string str;
            try
            {

                Hotel.spisok.Clear();


                while (!sr.EndOfStream)//Свойство,которое необходимо для получения значения, определяющее, находится ли позиция текущего потока в конце потока.
                {

                    str = sr.ReadLine(); //считывание строки из файла
                    A = str.Split(';'); //запись в массив значений полей
                    hot = new Hotel(A[0], int.Parse(A[1]), A[2], double.Parse(A[3]));
                    Hotel.spisok.Add(hot);

                }
                sr.Close();


            }

            catch
            { MessageBox.Show("Ошибка!"); }
        }
            public static void load()
       {

           Hotel hot;
           string[] A = new string[4]; string str;
           FileStream fs = new FileStream("Baza.txt", FileMode.OpenOrCreate);
           StreamReader sr = new StreamReader(fs, Encoding.Default);
           try
           {
               Hotel.spisok.Clear();
               while (!sr.EndOfStream)
               {
                   str = sr.ReadLine(); 
                   A = str.Split(';'); 
                   hot = new Hotel(A[0], int.Parse(A[1]), A[2], double.Parse(A[3]));
                   Hotel.spisok.Add(hot);
               }
               sr.Close();
           }
           catch
           { MessageBox.Show("Ошибка при открытии!"); }
        }
        
        public static void Save(List<Hotel>hot)
        {
           

            StreamWriter sw = new StreamWriter("Baza.txt", false, Encoding.Default);
            for (int i = 0; i < Hotel.spisok.Count; i++)//Цикл для сохранения
            {
                sw.WriteLine(Hotel.spisok[i].Name + ";" + Hotel.spisok[i].Stars + ";" + Hotel.spisok[i].Room + ";" + Hotel.spisok[i].Price);
            }
            sw.Close();
            load();
            }
    
 
    }
}
